package club.yuit.desktop;

import club.yuit.desktop.editor.LogEditorPane;
import club.yuit.rmq.service.ProducerService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.apis.message.Message;
import org.apache.rocketmq.client.apis.producer.Producer;
import org.apache.rocketmq.client.apis.producer.SendReceipt;

import javax.swing.text.*;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.concurrent.CompletableFuture;

/**
 * @author: yuit
 * @date: 2023/11/29 16:18
 */
@Slf4j
public class ProducerDataHandler {

    private LogEditorPane txt;
    private StyledDocument document;
    private String topic;

    private Keymap keymap;
    private Producer producer;
    private ProducerService producerService;
    private Element root;


    private final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");;


    public ProducerDataHandler(LogEditorPane txt, String topic, ProducerService producerService) {
        this.txt = txt;
        this.topic = topic;
        this.producerService = producerService;
        this.producer = producerService.normalProducer();
        this.document = txt.getStyledDocument();
        this.root = this.document.getDefaultRootElement();


    }




    public CompletableFuture<SendReceipt> senMsg(Message message){
        return producer.sendAsync(message);
    }


    public void closeProducer(){
        try {
            producer.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


}
